package lanbo.cj.lmp.utils;

import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;
import lanbo.cj.lmp.BaseApplication;

import java.util.List;

/**
 * Created by white on 15-9-21.
 */
public class HttpUtils<T>{
    private List<T> targets;
    private boolean updateResult;
    /**
     * 查询工具
     * @param key 查询条件的关键值
     * @param value 查询条件的确切值
     *@return 若成功返回查询结果，失败则返回null
     * */
    public List<T> Query(String key,String value){
        BmobQuery<T> query=new BmobQuery<T>();
        query.addWhereEqualTo(key,value);
        query.findObjects(BaseApplication.getInstance(), new FindListener<T>() {
            @Override
            public void onSuccess(List<T> list) {
                 targets = list;
            }

            @Override
            public void onError(int i, String s) {
                targets = null;
            }
        });
        return targets;
    }

    /**
     * 更新工具
     * @param  target 更新的BmobObject对象
     * @return 1为true；２为false
     * */
    public  boolean Update(BmobObject target){
        target.update(BaseApplication.getInstance(), new UpdateListener() {
            @Override
            public void onSuccess() {
                updateResult = true;
            }

            @Override
            public void onFailure(int i, String s) {
                updateResult = false;
            }
        });
        return updateResult;
    }
}
